home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 932 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.8 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Exceptions vs. assertions
  5. Date: 8 Jan 1996 14:34:27 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4cra1j$9oh@dawn.mmm.com>
  8. References: <4al1hn$73e@news.nstn.ca> <4bpt9b$cb5@news2.ios.com> <4ch1is$ldg@gold.datalytics.com>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Rob Stewart (stew@datalytics.com) wrote:
  13. > Exceptions aren't free.  Typically, each try block you code 
  14. > causes a performance hit, so limit the number of try blocks 
  15. > you use the best you can.  When you throw an exception, much 
  16. > occurs behind the scenes.  Throwing an exception is more 
  17. > expensive (in performance terms) than a simply function 
  18. > return.  If you can throw an exception for several different 
  19. > reasons, you may still need to decode the reason in the catch 
  20. > block, so that isn't any different from decoding an error code 
  21. > returned by a function.
  22.  
  23. The implementation of exceptions is very system dependent.  It
  24. is dangerous to make general assumptions based on one vendor's
  25. implementation of them.
  26.  
  27. People have sent me results of tests run on a dozen or so
  28. compilers.  On average, when no errors occured, code that
  29. contained try blocks performed at 95% of the level of equivalent
  30. code using return values.  Many try-blocks can be avoided by
  31. using resource-releasing classes.  To me, this makes the code
  32. easier to read.  I avoid them for this reason, but not because
  33. of performance.
  34.  
  35. > As a result, you should limit your use of exceptions.  Don't 
  36. > be afraid of them, but don't throw them when a simple result 
  37. > code would suffice, unless the sort of error you're reporting 
  38. > is quite likely going to unwind a great deal of functions 
  39. > calls from the stack.  Such an error is probably of the 
  40. > "catastrophic" variety anyway.
  41.  
  42. Performance is a very important consideration when errors *do*
  43. occur.  The same tests showed that, on average, the exception
  44. version of code performed at only 30-40% of the level of
  45. equivalent return value code (2-3 times as slow) when errors
  46. did occur.
  47.  
  48. When deciding how to report an error, I consider not so much
  49. whether a return code would suffice, but how likely it is
  50. that the condition I'm reporting will actually occur.
  51.  
  52. For any given function, this can be very dependent on the
  53. context of its use.  For example, consider the function
  54.     FileHandle openFile(const char* name);
  55. In the context of "saving to disk" it is probably an [unlikely]
  56. error if this occurs.  In the context of "reading optional
  57. resources" the file might be missing a significant portion of
  58. the time.  Because throwing exceptions is expensive in terms
  59. of performance, I would probably use a return value if the
  60. condition to be reported is "normal" in a significant portion
  61. of the function's uses.
  62.  
  63. > For example, a disk full condition is likely to unwind 
  64. > numerous functions back to some common point at which the 
  65. > program notifies the user of the condition and asks how to 
  66. > proceed.  The program may allow the user to repeat the action 
  67. > (hopefully after the user deleted some files from the full 
  68. > volume).  You could return disk full as an error code and pass 
  69. > it back the call chain until you reach the right point in the 
  70. > code to prompt the user.  Since there aren't likely to be many 
  71. > functions in the call stack that can handle it, "disk full" is 
  72. > a good candidate for an exception.
  73.  
  74. Whether exceptions or return values are used to report an error,
  75. it needs to be propagated to the code that actually handles it.
  76. How the error is handled has little to do with how it is reported.
  77. Good candidates for exceptions are situations that do not occur
  78. under normal circumstances.  The number of intervening functions
  79. in the call stack seems less important a factor in the decision.
  80.  
  81. However, error propagation with return values is a place where
  82. bugs are introduced.  Failing to propagate an error is much harder
  83. to do when exceptions are used.  In this way, depth of the call
  84. stack may be a factor in the decision on how to report an error.
  85.  
  86. > On the other hand, a more temporary failure might not be 
  87. > considered catastrophic, and could be handled closer to the 
  88. > troubled function call.  It really is subjective.  You can't 
  89. > even say, "I'm not sure what's a bad use of exceptions, but 
  90. > I'll know it when I see it."
  91.  
  92. I still think that the seriousness of the error and the placement
  93. of the error handler are determined independently of the error
  94. reporting mechanism.
  95. --
  96. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  97. 3M Company                      phone:  (612) 737-4643
  98. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  99. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  100.     But 3M speaks for me -- I did not write the following line:
  101.  
  102. Opinions expressed herein are my own and may not represent those of 3M.
  103.